home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 1.5)
-
- verbose
- join
- randint
- SHIFT = 15
- BASE = 2 ** SHIFT
- MASK = BASE - 1
- MAXDIGITS = 10
- special = map(long, [
- 0,
- 1,
- 2,
- BASE,
- BASE >> 1])
- special.append(0x5555555555555555L)
- special.append(0xAAAAAAAAAAAAAAAAL)
- p2 = 0x4L
- for i in range(2 * SHIFT):
- special.append(p2 - 1)
- p2 = p2 << 1
-
- del p2
- special = special + map((lambda x: ~x), special) + map((lambda x: -x), special)
-
- def check(ok, *args):
- if not ok:
- raise TestFailed, join(map(str, args), ' ')
-
-
-
- def getran(ndigits):
- if not __debug__ and ndigits > 0:
- raise AssertionError
- nbits_hi = ndigits * SHIFT
- nbits_lo = (nbits_hi - SHIFT) + 1
- answer = 0x0L
- nbits = 0
- r = int(random() * SHIFT * 2) | 1
- while nbits < nbits_lo:
- bits = (r >> 1) + 1
- bits = min(bits, nbits_hi - nbits)
- if __debug__:
- if bits <= bits:
- pass
- elif not bits <= SHIFT:
- raise AssertionError
- nbits = nbits + bits
- answer = answer << bits
- if r & 1:
- answer = answer | (1 << bits) - 1
-
- r = int(random() * SHIFT * 2)
- if __debug__:
- if nbits <= nbits:
- pass
- elif not nbits <= nbits_hi:
- raise AssertionError
- if random() < 0.5:
- answer = -answer
-
- return answer
-
-
- def getran2(ndigits):
- answer = 0x0L
- for i in range(ndigits):
- answer = answer << SHIFT | randint(0, MASK)
-
- return answer
-
-
- def test_division_2(x, y):
- (q, r) = divmod(x, y)
- (q2, r2) = (x / y, x % y)
- check(q == q2, 'divmod returns different quotient than / for', x, y)
- check(r == r2, 'divmod returns different mod than % for', x, y)
- check(x == q * y + r, 'x != q*y + r after divmod on', x, y)
- if y > 0:
- None(check if r <= r else r < y, 'bad mod from divmod on', x, y)
- elif r < r:
- pass
-
- check(r <= 0, 'bad mod from divmod on', x, y)
-
-
- def test_division(maxdigits = MAXDIGITS):
- print 'long / * % divmod'
- digits = range(1, maxdigits + 1)
- for lenx in digits:
- x = getran(lenx)
- for leny in digits:
- if not getran(leny):
- pass
- y = 0x1L
- test_division_2(x, y)
-
-
-
-
- def test_bitop_identities_1(x):
- check(x & 0 == 0, 'x & 0 != 0 for', x)
- check(x | 0 == x, 'x | 0 != x for', x)
- check(x ^ 0 == x, 'x ^ 0 != x for', x)
- check(x & -1 == x, 'x & -1 != x for', x)
- check(x | -1 == -1, 'x | -1 != -1 for', x)
- check(x ^ -1 == ~x, 'x ^ -1 != ~x for', x)
- check(x == ~~x, 'x != ~~x for', x)
- check(x & x == x, 'x & x != x for', x)
- check(x | x == x, 'x | x != x for', x)
- check(x ^ x == 0, 'x ^ x != 0 for', x)
- check(x & ~x == 0, 'x & ~x != 0 for', x)
- check(x | ~x == -1, 'x | ~x != -1 for', x)
- check(x ^ ~x == -1, 'x ^ ~x != -1 for', x)
- None(check if 1 + ~x == 1 + ~x else 1 + ~x == ~(x - 1), 'not -x == 1 + ~x == ~(x-1) for', x)
- for n in range(2 * SHIFT):
- p2 = 0x2L ** n
- check(x << n >> n == x, 'x << n >> n != x for', x, n)
- check(x / p2 == x >> n, 'x / p2 != x >> n for x n p2', x, n, p2)
- check(x * p2 == x << n, 'x * p2 != x << n for x n p2', x, n, p2)
- 0(check if (x >> n) << n == (x >> n) << n else (x >> n) << n == x & ~(p2 - 1), 'not x & -p2 == x >> n << n == x & ~(p2 - 1) for x n p2', x, n, p2)
-
-
-
- def test_bitop_identities_2(x, y):
- check(x & y == y & x, 'x & y != y & x for', x, y)
- check(x | y == y | x, 'x | y != y | x for', x, y)
- check(x ^ y == y ^ x, 'x ^ y != y ^ x for', x, y)
- check(x ^ y ^ x == y, 'x ^ y ^ x != y for', x, y)
- check(x & y == ~(~x | ~y), 'x & y != ~(~x | ~y) for', x, y)
- check(x | y == ~(~x & ~y), 'x | y != ~(~x & ~y) for', x, y)
- check(x ^ y == (x | y) & ~(x & y), 'x ^ y != (x | y) & ~(x & y) for', x, y)
- check(x ^ y == x & ~y | ~x & y, 'x ^ y == (x & ~y) | (~x & y) for', x, y)
- check(x ^ y == (x | y) & (~x | ~y), 'x ^ y == (x | y) & (~x | ~y) for', x, y)
-
-
- def test_bitop_identities_3(x, y, z):
- check(x & y & z == x & y & z, '(x & y) & z != x & (y & z) for', x, y, z)
- check(x | y | z == x | y | z, '(x | y) | z != x | (y | z) for', x, y, z)
- check(x ^ y ^ z == x ^ y ^ z, '(x ^ y) ^ z != x ^ (y ^ z) for', x, y, z)
- check(x & (y | z) == x & y | x & z, 'x & (y | z) != (x & y) | (x & z) for', x, y, z)
- check(x | y & z == (x | y) & (x | z), 'x | (y & z) != (x | y) & (x | z) for', x, y, z)
-
-
- def test_bitop_identities(maxdigits = MAXDIGITS):
- print 'long bit-operation identities'
- for x in special:
- test_bitop_identities_1(x)
-
- digits = range(1, maxdigits + 1)
- for lenx in digits:
- x = getran(lenx)
- test_bitop_identities_1(x)
- for leny in digits:
- y = getran(leny)
- test_bitop_identities_2(x, y)
- test_bitop_identities_3(x, y, getran((lenx + leny) / 2))
-
-
-
-
- def slow_format(x, base):
- if (x, base) == (0, 8):
- return '0L'
-
- digits = []
- sign = 0
- if x < 0:
- (sign, x) = (1, -x)
-
- while x:
- (x, r) = divmod(x, base)
- digits.append(int(r))
- digits.reverse()
- if not digits:
- pass
- digits = [
- 0]
- return '-'[:sign] + {
- 8: '0',
- 10: '',
- 16: '0x' }[base] + join(map((lambda i: '0123456789ABCDEF'[i]), digits), '') + 'L'
-
-
- def test_format_1(x):
- atol
- for base, mapper in ((8, oct), (10, str), (16, hex)):
- got = mapper(x)
- expected = slow_format(x, base)
- check(got == expected, mapper.__name__, 'returned', got, 'but expected', expected, 'for', x)
- check(atol(got, 0) == x, 'atol("%s", 0) !=' % got, x)
-
-
-
- def test_format(maxdigits = MAXDIGITS):
- print 'long str/hex/oct/atol'
- for x in special:
- test_format_1(x)
-
- for i in range(10):
- for lenx in range(1, maxdigits + 1):
- x = getran(lenx)
- test_format_1(x)
-
-
-
-
- def test_misc(maxdigits = MAXDIGITS):
- print 'long miscellaneous operations'
- import sys
- hugepos = sys.maxint
- hugeneg = -hugepos - 1
- hugepos_aslong = long(hugepos)
- hugeneg_aslong = long(hugeneg)
- check(hugepos == hugepos_aslong, 'long(sys.maxint) != sys.maxint')
- check(hugeneg == hugeneg_aslong, 'long(-sys.maxint-1) != -sys.maxint-1')
-
- try:
- check(int(hugepos_aslong) == hugepos, 'converting sys.maxint to long and back to int fails')
- except OverflowError:
- raise TestFailed, 'int(long(sys.maxint)) overflowed!'
-
-
- try:
- check(int(hugeneg_aslong) == hugeneg, 'converting -sys.maxint-1 to long and back to int fails')
- except OverflowError:
- raise TestFailed, 'int(long(-sys.maxint-1)) overflowed!'
-
- x = hugepos_aslong + 1
-
- try:
- int(x)
- raise ValueError
- except OverflowError:
- pass
- except:
- raise TestFailed, "int(long(sys.maxint) + 1) didn't overflow"
-
- x = hugeneg_aslong - 1
-
- try:
- int(x)
- raise ValueError
- except OverflowError:
- pass
- except:
- raise TestFailed, "int(long(-sys.maxint-1) - 1) didn't overflow"
-
-
- test_division()
- test_bitop_identities()
- test_format()
- test_misc()
-